package com.vincent.netty;

import io.netty.util.HashedWheelTimer;
import io.netty.util.Timeout;

import java.io.Serializable;
import java.lang.reflect.Constructor;
import java.time.LocalTime;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
 * Description
 *
 * @author yangwenchang
 * @version 1.0
 * @date 2018-02-24
 */
public class Test {

    int id;
    String name;
    //public Test() {
    //    System.out.println("Test");
    //}
    //public Test(int id,String name){
    //    this.id = id;
    //    this.name = name;
    //}
    public void say(){
        System.out.println("Hello!");
    }

    @Override
    public String toString() {
        return "id:"+id+","+"name:"+name+";";
    }

    public static void main(String[] args) throws Exception{
       //Class clazz =  Class.forName("com.vincent.netty.Test");
       //Object o = clazz.newInstance();
       //System.out.println(o);
       // Class c =  Test.class;
       // Constructor constructor = c.getDeclaredConstructor(int.class,String.class);
       // Object o = constructor.newInstance(1,"222");
       // System.out.println(o);
        Test t = new Test();
        System.out.println(t);

        t.testHashedWheelTimer();


    }

    /**
     * ﻿tickDuration: 每 tick 一次的时间间隔, 每 tick 一次就会到达下一个槽位
     * ticksPerWheel: 轮中的 slot 数
     */
    public void testHashedWheelTimer() throws InterruptedException {
        HashedWheelTimer hashedWheelTimer = new HashedWheelTimer(1000/**tickDuration**/, TimeUnit.MILLISECONDS, 16 /**ticksPerWheel**/);
        System.out.println(LocalTime.now()+" submitted");
        Timeout timeout = hashedWheelTimer.newTimeout((t) -> {
            new Thread(){
                @Override
                public void run() {
                    System.out.println(new Date() + " executed");
                    System.out.println(hashedWheelTimer);
                    try {
                        TimeUnit.SECONDS.sleep(100);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(new Date() + " FINISH");
                }
            }.start();
        }, 5, TimeUnit.SECONDS);

        hashedWheelTimer.newTimeout((t) -> {
            new Thread(){
                @Override
                public void run() {
                    System.out.println(new Date() + " TASK2 executed");
                    System.out.println(hashedWheelTimer);
                    try {
                        TimeUnit.SECONDS.sleep(10);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    System.out.println(new Date() + " TASK2 FINISH");
                }
            }.start();
        }, 15, TimeUnit.SECONDS);

        TimeUnit.SECONDS.sleep(500);
    }
}

